"""
分页类的实现
from utils import pagination
    current_page = request.GET.get('p', 1)
    current_page = int(current_page)
    page_obj = pagination.Page(current_page, len(LIST))
    data = LIST[page_obj.start:page_obj.end]
    page_str = page_obj.page_srt('/cmdb/user_list3')
    return render(request, 'user_list.html', {'li': data, 'page_str': page_str})
"""
from django.utils.safestring import mark_safe


class Page:
    def __init__(self, current_page, data_count, per_page_count=10, pager_num=7):
        self.current_page = current_page
        self.data_count = data_count
        self.per_page_count = per_page_count
        self.pager_num = pager_num

    @property
    def start(self):
        """
        返回开始页
        :return:
        """
        return (self.current_page-1) * self.per_page_count

    @property
    def end(self):
        """
        返回结束页
        :return:
        """
        return self.current_page * self.per_page_count

    @property
    def all_count(self):
        v, y = divmod(self.data_count, self.per_page_count)
        if y:
            v += 1
        return v

    def page_srt(self, base_url):
        page_list = []
        pager_num = 5
        # 当前页
        if self.all_count < pager_num:
            start_index = 1
            end_index = self.all_count + 1
        else:
            if self.current_page <= (self.pager_num + 1) / 2:
                start_index = 1
                end_index = self.pager_num + 1
            else:
                start_index = self.current_page - (self.pager_num - 1) / 2
                end_index = self.current_page + (self.pager_num + 1) / 2
                if (self.current_page + (self.pager_num - 1) / 2) > self.all_count:
                    end_index = self.all_count + 1
                    start_index = self.all_count - self.pager_num + 1
        if self.current_page == 1:
            prev = '<a class="page" href="#">上一页</a>'
        else:
            prev = '<a class="page" href="%s?p=%s">上一页</a>' % (base_url, self.current_page - 1,)
        page_list.append(prev)
        for i in range(int(start_index), int(end_index)):
            if i == self.current_page:
                temps = '<a class="page active" href="%s?p=%s">%s</a>' % (base_url, i, i)
            else:
                temps = '<a class="page" href="%s?p=%s">%s</a>' % (base_url, i, i)
            page_list.append(temps)
        if self.current_page == self.all_count:
            nex = '<a class="page" href="javascript:void(0);">下一页</a>'
        else:
            nex = '<a class="page" href="%s?p=%s">下一页</a>' % (base_url, self.current_page + 1,)
        page_list.append(nex)
        jump = """
        <label>
            <input type='text'/><a onclick='jumpTo(this, "%s?p=");'>Go</a>
        </label>
        <script>
            function jumpTo(ths,base){
                const val = ths.previousSibling.value;
                location.href = base + val;
            }
        </script>
        """ % (base_url,)
        page_list.append(jump)
        page_str = "".join(page_list)
        page_str = mark_safe(page_str)
        return page_str
